package com.qf.nacos.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.qf.nacos.client.ProducerClient;
import com.qf.nacos.controller.fallback.consumerControllerFallback;
import com.qf.nacos.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope   // 代表当前类有 需要实现的动态配置刷新的值,
public class ConsumerController {

    @Autowired
    private ProducerClient producerClient;

    @Value("${version}")
    private String version;

    @Autowired
    private ConsumerService consumerService;


    @GetMapping("/transfer")
    public String transfer(){
        consumerService.transfer();
        return "SUCCESS!";
    }


    @GetMapping("/consumer/test")
    @SentinelResource(value = "test",defaultFallback = "testFallback",fallbackClass = consumerControllerFallback.class)
    public String test(String id,String name,Integer age) throws InterruptedException {
        if("0".equalsIgnoreCase(id)) {
            Thread.sleep(1000);
        }
        if("1".equalsIgnoreCase(id)){
            int i = 1 / 0;
        }
        String result = producerClient.test();
        return result + ",version:" + version;
    }

    public String testBlock(String id,String name,Integer age,BlockException ex) throws InterruptedException {
        return "block ....." + ex.getMessage();
    }

//    public String testFallback(String id,String name,Integer age,Throwable ex) throws InterruptedException {
//        return "fallback ....." + ex.getMessage();
//    }







    @GetMapping("/testA")
    public String testA(){
        return consumerService.testA();
    }

    @GetMapping("/testB")
    public String testB(){
        return consumerService.testA();
    }








}
